package com.jlt.service.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jlt.dao.sys.SysParamDao;
import com.jlt.entity.sys.SysParam;

/**
 * 参数对象相关实体的管理类,.
 * 
 * @author zhoutao
 */
//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class SysParamManager {

	private static Logger logger = LoggerFactory.getLogger(SysParamManager.class);

	private SysParamDao sysParamDao;
	
	/**
	 * 装载参数
	 * @param  code 参数代码
	 * @return SysParam 参数对象
	 */
	@Transactional(readOnly = true)
	public String getSysParamByCode(String code) {
		logger.info("load sysParam:"+code);
		SysParam sp = sysParamDao.findUniqueBy("code", code);
		return sp == null?"":sp.getParamValue();
	}
	
	/**
	 * 装载参数
	 * @param  code 参数代码
	 * @return  Map<code+id , value>参数对象 Map 
	 */
	@Transactional(readOnly = true)
	public Map<String , String> findSysParamByType(String type) {
		logger.info("load sysParam List:"+type);
		Map<String,String> map = new HashMap<String, String>();
		List<SysParam> list = sysParamDao.findBy("type", type);
		for (SysParam sysParam : list){
			map.put(sysParam.getCode(), sysParam.getParamValue());
		}
		return map;
	}
	
	@Autowired
	public void setSysParam(SysParamDao sysParamDao) {
		this.sysParamDao = sysParamDao;
	}
}
